home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / lib / python2.5 / site.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2007-05-11  |  14.1 KB  |  501 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.5)
  3.  
  4. """Append module search paths for third-party packages to sys.path.
  5.  
  6. ****************************************************************
  7. * This module is automatically imported during initialization. *
  8. ****************************************************************
  9.  
  10. In earlier versions of Python (up to 1.5a3), scripts or modules that
  11. needed to use site-specific modules would place ``import site''
  12. somewhere near the top of their code.  Because of the automatic
  13. import, this is no longer necessary (but code that does it still
  14. works).
  15.  
  16. This will append site-specific paths to the module search path.  On
  17. Unix (including Mac OSX), it starts with sys.prefix and
  18. sys.exec_prefix (if different) and appends
  19. lib/python<version>/site-packages as well as lib/site-python.
  20. On other platforms (such as Windows), it tries each of the
  21. prefixes directly, as well as with lib/site-packages appended.  The
  22. resulting directories, if they exist, are appended to sys.path, and
  23. also inspected for path configuration files.
  24.  
  25. FOR DEBIAN, this sys.path is augmented with directories in /usr/local.
  26. Local addons go into /usr/local/lib/python<version>/site-packages
  27. (resp. /usr/local/lib/site-python), Debian addons install into
  28. /usr/{lib,share}/python<version>/site-packages.
  29.  
  30. A path configuration file is a file whose name has the form
  31. <package>.pth; its contents are additional directories (one per line)
  32. to be added to sys.path.  Non-existing directories (or
  33. non-directories) are never added to sys.path; no directory is added to
  34. sys.path more than once.  Blank lines and lines beginning with
  35. '#' are skipped. Lines starting with 'import' are executed.
  36.  
  37. For example, suppose sys.prefix and sys.exec_prefix are set to
  38. /usr/local and there is a directory /usr/local/lib/python2.5/site-packages
  39. with three subdirectories, foo, bar and spam, and two path
  40. configuration files, foo.pth and bar.pth.  Assume foo.pth contains the
  41. following:
  42.  
  43.   # foo package configuration
  44.   foo
  45.   bar
  46.   bletch
  47.  
  48. and bar.pth contains:
  49.  
  50.   # bar package configuration
  51.   bar
  52.  
  53. Then the following directories are added to sys.path, in this order:
  54.  
  55.   /usr/local/lib/python2.5/site-packages/bar
  56.   /usr/local/lib/python2.5/site-packages/foo
  57.  
  58. Note that bletch is omitted because it doesn't exist; bar precedes foo
  59. because bar.pth comes alphabetically before foo.pth; and spam is
  60. omitted because it is not mentioned in either path configuration file.
  61.  
  62. After these path manipulations, an attempt is made to import a module
  63. named sitecustomize, which can perform arbitrary additional
  64. site-specific customizations.  If this import fails with an
  65. ImportError exception, it is silently ignored.
  66.  
  67. """
  68. import sys
  69. import os
  70. import __builtin__
  71.  
  72. def makepath(*paths):
  73.     dir = os.path.abspath(os.path.join(*paths))
  74.     return (dir, os.path.normcase(dir))
  75.  
  76.  
  77. def abs__file__():
  78.     """Set all module' __file__ attribute to an absolute path"""
  79.     for m in sys.modules.values():
  80.         if hasattr(m, '__loader__'):
  81.             continue
  82.         
  83.         
  84.         try:
  85.             m.__file__ = os.path.abspath(m.__file__)
  86.         continue
  87.         except AttributeError:
  88.             continue
  89.             continue
  90.         
  91.  
  92.     
  93.  
  94.  
  95. def removeduppaths():
  96.     ''' Remove duplicate entries from sys.path along with making them
  97.     absolute'''
  98.     L = []
  99.     known_paths = set()
  100.     for dir in sys.path:
  101.         (dir, dircase) = makepath(dir)
  102.         if dircase not in known_paths:
  103.             L.append(dir)
  104.             known_paths.add(dircase)
  105.             continue
  106.     
  107.     sys.path[:] = L
  108.     return known_paths
  109.  
  110.  
  111. def addbuilddir():
  112.     """Append ./build/lib.<platform> in case we're running in the build dir
  113.     (especially for Guido :-)"""
  114.     get_platform = get_platform
  115.     import distutils.util
  116.     if not sys.pydebug or '_d':
  117.         pass
  118.     s = 'build/lib%s.%s-%.3s' % ('', get_platform(), sys.version)
  119.     s = os.path.join(os.path.dirname(sys.path[-1]), s)
  120.     sys.path.append(s)
  121.  
  122.  
  123. def _init_pathinfo():
  124.     '''Return a set containing all existing directory entries from sys.path'''
  125.     d = set()
  126.     for dir in sys.path:
  127.         
  128.         try:
  129.             if os.path.isdir(dir):
  130.                 (dir, dircase) = makepath(dir)
  131.                 d.add(dircase)
  132.         continue
  133.         except TypeError:
  134.             continue
  135.             continue
  136.         
  137.  
  138.     
  139.     return d
  140.  
  141.  
  142. def addpackage(sitedir, name, known_paths):
  143.     """Add a new path to known_paths by combining sitedir and 'name' or execute
  144.     sitedir if it starts with 'import'"""
  145.     if known_paths is None:
  146.         _init_pathinfo()
  147.         reset = 1
  148.     else:
  149.         reset = 0
  150.     fullname = os.path.join(sitedir, name)
  151.     
  152.     try:
  153.         f = open(fullname, 'rU')
  154.     except IOError:
  155.         return None
  156.  
  157.     
  158.     try:
  159.         for line in f:
  160.             if line.startswith('#'):
  161.                 continue
  162.             
  163.             if line.startswith('import'):
  164.                 exec line
  165.                 continue
  166.             
  167.             line = line.rstrip()
  168.             (dir, dircase) = makepath(sitedir, line)
  169.             if dircase not in known_paths and os.path.exists(dir):
  170.                 sys.path.append(dir)
  171.                 known_paths.add(dircase)
  172.                 continue
  173.     finally:
  174.         f.close()
  175.  
  176.     if reset:
  177.         known_paths = None
  178.     
  179.     return known_paths
  180.  
  181.  
  182. def addsitedir(sitedir, known_paths = None):
  183.     """Add 'sitedir' argument to sys.path if missing and handle .pth files in
  184.     'sitedir'"""
  185.     if known_paths is None:
  186.         known_paths = _init_pathinfo()
  187.         reset = 1
  188.     else:
  189.         reset = 0
  190.     (sitedir, sitedircase) = makepath(sitedir)
  191.     if sitedircase not in known_paths:
  192.         sys.path.append(sitedir)
  193.     
  194.     
  195.     try:
  196.         names = os.listdir(sitedir)
  197.     except os.error:
  198.         return None
  199.  
  200.     names.sort()
  201.     for name in names:
  202.         if name.endswith(os.extsep + 'pth'):
  203.             addpackage(sitedir, name, known_paths)
  204.             continue
  205.     
  206.     if reset:
  207.         known_paths = None
  208.     
  209.     return known_paths
  210.  
  211.  
  212. def addsitepackages(known_paths):
  213.     '''Add site-packages (and possibly site-python) to sys.path'''
  214.     prefixes = [
  215.         os.path.join(sys.prefix, 'local'),
  216.         sys.prefix]
  217.     if sys.exec_prefix != sys.prefix:
  218.         prefixes.append(os.path.join(sys.exec_prefix, 'local'))
  219.     
  220.     for prefix in prefixes:
  221.         if prefix:
  222.             if sys.platform in ('os2emx', 'riscos'):
  223.                 sitedirs = [
  224.                     os.path.join(prefix, 'Lib', 'site-packages')]
  225.             elif os.sep == '/':
  226.                 sitedirs = [
  227.                     os.path.join(prefix, 'lib', 'python' + sys.version[:3], 'site-packages'),
  228.                     os.path.join(prefix, 'lib', 'site-python')]
  229.             else:
  230.                 sitedirs = [
  231.                     prefix,
  232.                     os.path.join(prefix, 'lib', 'site-packages')]
  233.             if sys.platform == 'darwin':
  234.                 if 'Python.framework' in prefix:
  235.                     home = os.environ.get('HOME')
  236.                     if home:
  237.                         sitedirs.append(os.path.join(home, 'Library', 'Python', sys.version[:3], 'site-packages'))
  238.                     
  239.                 
  240.             
  241.             for sitedir in sitedirs:
  242.                 if os.path.isdir(sitedir):
  243.                     addsitedir(sitedir, known_paths)
  244.                     continue
  245.             
  246.     
  247.  
  248.  
  249. def setBEGINLIBPATH():
  250.     '''The OS/2 EMX port has optional extension modules that do double duty
  251.     as DLLs (and must use the .DLL file extension) for other extensions.
  252.     The library search path needs to be amended so these will be found
  253.     during module import.  Use BEGINLIBPATH so that these are at the start
  254.     of the library search path.
  255.  
  256.     '''
  257.     dllpath = os.path.join(sys.prefix, 'Lib', 'lib-dynload')
  258.     libpath = os.environ['BEGINLIBPATH'].split(';')
  259.     if libpath[-1]:
  260.         libpath.append(dllpath)
  261.     else:
  262.         libpath[-1] = dllpath
  263.     os.environ['BEGINLIBPATH'] = ';'.join(libpath)
  264.  
  265.  
  266. def setquit():
  267.     """Define new built-ins 'quit' and 'exit'.
  268.     These are simply strings that display a hint on how to exit.
  269.  
  270.     """
  271.     if os.sep == ':':
  272.         eof = 'Cmd-Q'
  273.     elif os.sep == '\\':
  274.         eof = 'Ctrl-Z plus Return'
  275.     else:
  276.         eof = 'Ctrl-D (i.e. EOF)'
  277.     
  278.     class Quitter((object,)):
  279.         
  280.         def __init__(self, name):
  281.             self.name = name
  282.  
  283.         
  284.         def __repr__(self):
  285.             return 'Use %s() or %s to exit' % (self.name, eof)
  286.  
  287.         
  288.         def __call__(self, code = None):
  289.             
  290.             try:
  291.                 sys.stdin.close()
  292.             except:
  293.                 pass
  294.  
  295.             raise SystemExit(code)
  296.  
  297.  
  298.     __builtin__.quit = Quitter('quit')
  299.     __builtin__.exit = Quitter('exit')
  300.  
  301.  
  302. class _Printer(object):
  303.     '''interactive prompt objects for printing the license text, a list of
  304.     contributors and the copyright notice.'''
  305.     MAXLINES = 23
  306.     
  307.     def __init__(self, name, data, files = (), dirs = ()):
  308.         self._Printer__name = name
  309.         self._Printer__data = data
  310.         self._Printer__files = files
  311.         self._Printer__dirs = dirs
  312.         self._Printer__lines = None
  313.  
  314.     
  315.     def _Printer__setup(self):
  316.         if self._Printer__lines:
  317.             return None
  318.         
  319.         data = None
  320.         for dir in self._Printer__dirs:
  321.             for filename in self._Printer__files:
  322.                 filename = os.path.join(dir, filename)
  323.                 
  324.                 try:
  325.                     fp = file(filename, 'rU')
  326.                     data = fp.read()
  327.                     fp.close()
  328.                 continue
  329.                 except IOError:
  330.                     continue
  331.                 
  332.  
  333.             
  334.             if data:
  335.                 break
  336.                 continue
  337.             None<EXCEPTION MATCH>IOError
  338.         
  339.         if not data:
  340.             data = self._Printer__data
  341.         
  342.         self._Printer__lines = data.split('\n')
  343.         self._Printer__linecnt = len(self._Printer__lines)
  344.  
  345.     
  346.     def __repr__(self):
  347.         self._Printer__setup()
  348.         if len(self._Printer__lines) <= self.MAXLINES:
  349.             return '\n'.join(self._Printer__lines)
  350.         else:
  351.             return 'Type %s() to see the full %s text' % (self._Printer__name,) * 2
  352.  
  353.     
  354.     def __call__(self):
  355.         self._Printer__setup()
  356.         prompt = 'Hit Return for more, or q (and Return) to quit: '
  357.         lineno = 0
  358.         while None:
  359.             
  360.             try:
  361.                 for i in range(lineno, lineno + self.MAXLINES):
  362.                     print self._Printer__lines[i]
  363.             except IndexError:
  364.                 break
  365.                 continue
  366.  
  367.             lineno += self.MAXLINES
  368.             key = None
  369.             while key is None:
  370.                 key = raw_input(prompt)
  371.                 if key not in ('', 'q'):
  372.                     key = None
  373.                     continue
  374.             if key == 'q':
  375.                 break
  376.                 continue
  377.             continue
  378.             return None
  379.  
  380.  
  381.  
  382. def setcopyright():
  383.     """Set 'copyright' and 'credits' in __builtin__"""
  384.     __builtin__.copyright = _Printer('copyright', sys.copyright)
  385.     if sys.platform[:4] == 'java':
  386.         __builtin__.credits = _Printer('credits', 'Jython is maintained by the Jython developers (www.jython.org).')
  387.     else:
  388.         __builtin__.credits = _Printer('credits', '    Thanks to CWI, CNRI, BeOpen.com, Zope Corporation and a cast of thousands\n    for supporting Python development.  See www.python.org for more information.')
  389.     here = os.path.dirname(os.__file__)
  390.     __builtin__.license = _Printer('license', 'See http://www.python.org/%.3s/license.html' % sys.version, [
  391.         'LICENSE.txt',
  392.         'LICENSE'], [
  393.         os.path.join(here, os.pardir),
  394.         here,
  395.         os.curdir])
  396.  
  397.  
  398. class _Helper(object):
  399.     """Define the built-in 'help'.
  400.     This is a wrapper around pydoc.help (with a twist).
  401.  
  402.     """
  403.     
  404.     def __repr__(self):
  405.         return 'Type help() for interactive help, or help(object) for help about object.'
  406.  
  407.     
  408.     def __call__(self, *args, **kwds):
  409.         import pydoc
  410.         return pydoc.help(*args, **kwds)
  411.  
  412.  
  413.  
  414. def sethelper():
  415.     __builtin__.help = _Helper()
  416.  
  417.  
  418. def aliasmbcs():
  419.     '''On Windows, some default encodings are not provided by Python,
  420.     while they are always available as "mbcs" in each locale. Make
  421.     them usable by aliasing to "mbcs" in such a case.'''
  422.     if sys.platform == 'win32':
  423.         import locale
  424.         import codecs
  425.         enc = locale.getdefaultlocale()[1]
  426.         if enc.startswith('cp'):
  427.             
  428.             try:
  429.                 codecs.lookup(enc)
  430.             except LookupError:
  431.                 import encodings
  432.                 encodings._cache[enc] = encodings._unknown
  433.                 encodings.aliases.aliases[enc] = 'mbcs'
  434.             except:
  435.                 None<EXCEPTION MATCH>LookupError
  436.             
  437.  
  438.         None<EXCEPTION MATCH>LookupError
  439.     
  440.  
  441.  
  442. def setencoding():
  443.     """Set the string encoding used by the Unicode implementation.  The
  444.     default is 'ascii', but if you're willing to experiment, you can
  445.     change this."""
  446.     encoding = 'ascii'
  447.     if encoding != 'ascii':
  448.         sys.setdefaultencoding(encoding)
  449.     
  450.  
  451.  
  452. def execsitecustomize():
  453.     '''Run custom site specific code, if available.'''
  454.     
  455.     try:
  456.         import sitecustomize
  457.     except ImportError:
  458.         pass
  459.  
  460.  
  461.  
  462. def main():
  463.     abs__file__()
  464.     paths_in_sys = removeduppaths()
  465.     if os.name == 'posix' and sys.path and os.path.basename(sys.path[-1]) == 'Modules':
  466.         addbuilddir()
  467.     
  468.     paths_in_sys = addsitepackages(paths_in_sys)
  469.     if sys.platform == 'os2emx':
  470.         setBEGINLIBPATH()
  471.     
  472.     setquit()
  473.     setcopyright()
  474.     sethelper()
  475.     aliasmbcs()
  476.     setencoding()
  477.     execsitecustomize()
  478.     if hasattr(sys, 'setdefaultencoding'):
  479.         del sys.setdefaultencoding
  480.     
  481.     
  482.     try:
  483.         import apport_python_hook
  484.     except ImportError:
  485.         pass
  486.  
  487.     apport_python_hook.install()
  488.  
  489. main()
  490.  
  491. def _test():
  492.     print 'sys.path = ['
  493.     for dir in sys.path:
  494.         print '    %r,' % (dir,)
  495.     
  496.     print ']'
  497.  
  498. if __name__ == '__main__':
  499.     _test()
  500.  
  501.